package com.nhc.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.location.GpsSatellite;
import android.util.AttributeSet;
import android.view.View;

import com.astrob.activitys.GPSProvider;
import com.astrob.util.SysUtils;

import java.text.DecimalFormat;
import java.util.Iterator;

//卫星强度
public class GPS2StarQDView extends View {

    private Iterable<GpsSatellite> mgpsAllStatus = null;

    // 屏大小
    private int mnHegiht = 800;
    private int mnWidth = 480;

    private Context context;

    public GPS2StarQDView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public GPS2StarQDView(Context context) {
        super(context);
        this.context = context;
    }

    private int dip2px(float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        mnHegiht = (bottom - top);
        mnWidth = (right - left);
    }

    public int getGPSNum() {
        mgpsAllStatus = GPSProvider.getInstance().GetSatellite();
        int count = 0;
        if (mgpsAllStatus == null) {
            return 0;
        }
        for (Iterator<GpsSatellite> it = mgpsAllStatus.iterator(); it
                .hasNext(); ) {
            GpsSatellite gpsState = it.next();
            int s = (int) gpsState.getSnr();
            if (s > 0) {
                count++;
            }
        }

        return count;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!SysUtils.getInstance().is240x320) {
            Paint mpaint = new Paint();
            mpaint.setAntiAlias(true);
            mpaint.setColor(Color.WHITE);
            mpaint.setStyle(Paint.Style.FILL);

            if (getGPSNum() < 1) {
                canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mpaint);
                return;
            }

            int y0 = dip2px(50);
            canvas.drawRect(0, y0, canvas.getWidth(), canvas.getHeight(), mpaint);
        }

        drawSatellites(canvas);

    }

    private void drawSatellites(Canvas canvas) {
        mgpsAllStatus = GPSProvider.getInstance().GetSatellite();
        if (null == mgpsAllStatus) {
//            for (int i = 0; i < 32; i++) {
//                drawSatelliteQD_240x320(canvas, null, i);
//            }
            return;
        }
        Iterator<GpsSatellite> it = mgpsAllStatus.iterator();
        int i = 0;
        while (it.hasNext()) {
            GpsSatellite gpsState = it.next();
            int s = (int) gpsState.getSnr();
            if (s <= 0) {
                continue;
            }
            if (SysUtils.getInstance().is240x320) {
                drawSatelliteQD_240x320(canvas, gpsState, i);
                i++;
            } else {
                drawSatelliteQD(canvas, gpsState, i);
                i++;
            }

        }

    }

    private void drawSatelliteQD(Canvas canvas, GpsSatellite gpsState, int i) {

        if (i >= 32) {
            return;
        }

        Paint mpaint = new Paint();
        mpaint.setAntiAlias(true);
        mpaint.setTextSize(dip2px(12));
        mpaint.setStyle(Paint.Style.FILL);

        int s = (int) gpsState.getSnr();
        if (s <= 0) {
            return;
        }

        if (gpsState.usedInFix()) {
            mpaint.setColor(Color.GREEN);
        } else {
            if (s >= 30)
                mpaint.setColor(Color.YELLOW);
            else
                mpaint.setColor(Color.RED);
        }

        int x0 = dip2px(15);
        int x = dip2px(1);//间隔

        int with = canvas.getWidth();

        int wx = (int) ((with - x0) / 32.0);
        int w = wx - x + 2;
        int h = s * dip2px(50) / 50;
        int y0 = dip2px(50);

        float left = x0 + i * w;
        float top = y0 - h;
        float right = left + w - x;
        float bottom = y0;
        if (left > canvas.getWidth() - x0) {
            return;
        }
        canvas.drawRect(left, top, right, bottom, mpaint);

        int prn = gpsState.getPrn();
        String strPrn = String.valueOf(gpsState.getPrn());

//        strPrn = (i + 1) + "";

        float textWidth = mpaint.measureText(strPrn) / 2;
        mpaint.setColor(Color.GREEN);

        drawText(canvas, strPrn, x0 + i * w + dip2px(10), y0 + dip2px(19), mpaint, -90);
        if (prn >= 200) {
            mpaint.setColor(Color.RED);
        } else {
            mpaint.setColor(Color.BLACK);
        }
        drawText(canvas, strPrn, x0 + i * w + dip2px(10), y0 + dip2px(20), mpaint, -90);

    }

    void drawText(Canvas canvas, String text, float x, float y, Paint paint, float angle) {
        if (angle != 0) {
            canvas.rotate(angle, x, y);
        }
        canvas.drawText(text, x, y, paint);
        if (angle != 0) {
            canvas.rotate(-angle, x, y);
        }
    }

    private void drawSatelliteQD_240x320(Canvas canvas, GpsSatellite gpsState, int i) {
        if (i >= 36) {
            return;
        }

        int s = 28 + i;
        String strPrn = (i + 1) + "";
        boolean isFixed = false;
        if (gpsState != null) {
            s = (int) gpsState.getSnr();
            isFixed = gpsState.usedInFix();
            strPrn = String.valueOf(gpsState.getPrn());
        }
        int y0 = canvas.getHeight() - 14;//12 -- 文字编号的高度
        if (i < 16) {
            y0 = canvas.getHeight() / 2 - 12;
        } else {
            i = i - 16;
        }
        Paint mpaint = new Paint();
        mpaint.setAntiAlias(true);
        mpaint.setTextSize(11);
        mpaint.setStyle(Paint.Style.FILL);

        if (isFixed) {
            mpaint.setColor(Color.GREEN);
        } else {
            if (s >= 30)
                mpaint.setColor(Color.YELLOW);
            else
                mpaint.setColor(Color.RED);
        }

        int w = dip2px(18);
        int h = s * dip2px(canvas.getHeight() / 2 - 12) / 50;//柱高
        int x0 = dip2px(15);
        int x = dip2px(4);//间隔

        float left = x0 + i * w;
        float top = y0 - h;
        float right = left + w - x;
        float bottom = y0;
        if (left > canvas.getWidth() - x0) {
            return;
        }
        canvas.drawRect(left, top, right, bottom, mpaint);

        int prn = gpsState.getPrn();
        if (prn >= 200) {
            //BD
            strPrn = new DecimalFormat("00").format(prn-200);
        }

        if (prn >= 200) {
            mpaint.setColor(Color.RED);
        } else {
            mpaint.setColor(Color.BLACK);
        }
        mpaint.setStyle(Paint.Style.STROKE);

        canvas.drawText(strPrn, x0 + i * w, y0 + 11, mpaint);

    }

}
